<?php

namespace App\Http\Controllers\Api\Mobile;

use Illuminate\Http\Request;
use Illuminate\Support\Facades\Cache;

use App\Models\Notice;

class NoticeController extends Controller
{
    public function index(Request $request){
        if($request->has('code') && $request->code){
            $code_data = decodeCode($request->code);
            //解析后的code是数组
            if(is_array($code_data) && count($code_data)>3){
                //正常逻辑代码
                $notices = Cache::rememberForever('user_notices',  function () {
                    //落地有可能没有了，所以加锁防止没有时缓存穿透
                    return Cache::lock('user_notices')->get(function () {
                        // 获取无限期锁并自动释放...
                        $notices = Notice::show()->user()->select('id','title','content')->orderBy('updated_at','desc')->get()->toArray();
                        if(count($notices)){
                            return $notices;
                        }else return [];
                    });
                });
                if(count($notices)<=0){
                    Cache::forget('user_notices');
                }
                $data['notices'] = $notices;
                return $this->success($data);
            }
            else{
                return $this->errMsg('有情况，请通过正常渠道访问！');
            }
        }
        return $this->errMsg('有情况，请通过正常渠道访问');
    }
}
